package com.example.demo.controller;

import com.example.demo.dto.PaginationDTO;
import com.example.demo.dto.QuestionDTO;
import com.example.demo.mapper.QuestionMapper;
import com.example.demo.mapper.UserMapper;
import com.example.demo.model.Question;
import com.example.demo.model.User;
import com.example.demo.server.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/***
 * Indexcontroller  首页代码
 * 保持登陆状态代码
 */
@Controller
public class IndexController {
    @Autowired
    private UserMapper userMapper;
//     问题的列表页面
    @Autowired(required = true)
    private QuestionService mQuestionService;

    @GetMapping("/")
    ///2021.7.14 分页
    public String index(HttpServletRequest request, Model model,
                        // 页  默认为1
                        @RequestParam(name="page",defaultValue = "1")Integer page,
//    每页展示默认5条数
                        @RequestParam(name="size",defaultValue = "2") Integer size
    ) {
        Cookie[] cookies = request.getCookies();

        if (cookies!=null&&cookies.length>0){// 判断cookies  不为null
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals("token")) {
                String token = cookie.getValue();
                User user = userMapper.findByToken(token);
                if (user != null) {
                    request.getSession().setAttribute("user", user);
                }
                break;
            }

        }}

//        d定义question
        System.out.println("page===="+page);
        System.out.println("size====="+size);
        PaginationDTO pagination=mQuestionService.list(page,size);
        model.addAttribute("pagination",pagination);
        System.out.println("IndexController+model"+model);
        System.out.println("pagination"+pagination);
//        for (int i=0; i<paginationdto.size(); i++){
//            System.out.println("CommentCount()="+questionList.get(i).getCommentCount());
//            System.out.println("getCreator()="+questionList.get(i).getCreator());
//            System.out.println("getDescription()="+questionList.get(i).getDescription());
//            System.out.println("getGmtCreate()="+questionList.get(i).getGmtCreate());
//            System.out.println("getGmtModified()="+questionList.get(i).getGmtModified());
//            System.out.println("getId()="+questionList.get(i).getId());
//            System.out.println("LikeCount="+questionList.get(i).getLikeCount());
//            System.out.println("tag="+questionList.get(i).getTags());
//            System.out.println("title="+questionList.get(i).getTitle());
//        }
        return "index";
    }
//    http://localhost:8887/css/community.css
//    上面方法可以验证css 文件
}
